package cn.com.bluemoon.daps.system.service.impl;

import cn.com.bluemoon.daps.common.toolkit.TreeUtils;
import cn.com.bluemoon.daps.system.entity.DapSystemWechatDept;
import cn.com.bluemoon.daps.system.mapper.DapSystemWechatDeptMapper;
import cn.com.bluemoon.daps.system.service.DapSystemWechatDeptService;
import cn.com.bluemoon.daps.system.vo.DapSystemWechatDeptTreeVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统企业微信部门数据 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-12-12
 */
@Slf4j
@Service
public class DapSystemWechatDeptServiceImpl extends ServiceImpl<DapSystemWechatDeptMapper, DapSystemWechatDept>
        implements DapSystemWechatDeptService {

    @Override
    public List<DapSystemWechatDeptTreeVo> getDapSystemWechatDeptTreeVos(Integer level) {
        List<DapSystemWechatDeptTreeVo> levelResult = new ArrayList<>();
        List<DapSystemWechatDept> depts = list();
        List<DapSystemWechatDeptTreeVo> res = depts.stream().map(d -> {
            DapSystemWechatDeptTreeVo each = new DapSystemWechatDeptTreeVo();
            BeanUtils.copyProperties(d, each);
            return each;
        }).collect(Collectors.toList());
        List<DapSystemWechatDeptTreeVo> trees = TreeUtils.createTrees(res, "id", "parentid", "nextNodes", null, null);
        if (level != null && level > 0) {
            int treeLevel = 1;
            findCustomLevelDeptReverse(trees, treeLevel, level, levelResult);
            for (DapSystemWechatDeptTreeVo each : levelResult) {
                each.setNextNodes(null);
            }
        } else {
            levelResult.addAll(trees);
        }
        return levelResult;
    }

    @Override
    public List<DapSystemWechatDept> getDapSystemWechatDeptByIds(List<Integer> ids) {
        if (ids.isEmpty()){
            return Collections.emptyList();
        }
        List<DapSystemWechatDept> res = list(Wrappers.<DapSystemWechatDept>lambdaQuery().in(DapSystemWechatDept::getId, ids));
        return res;
    }

    private void findCustomLevelDeptReverse(List<DapSystemWechatDeptTreeVo> trees,
                                            int treeLevel, final Integer level,
                                            List<DapSystemWechatDeptTreeVo> levelResult) {
        if (trees != null && trees.size() > 0) {
            if (level.equals(treeLevel)) {
                levelResult.addAll(trees);
                return;
            }
            for (DapSystemWechatDeptTreeVo each : trees) {
                each.setLevel(treeLevel);
                if (each.getNextNodes() != null && each.getNextNodes().size() > 0) {
                    findCustomLevelDeptReverse(each.getNextNodes(), treeLevel + 1, level, levelResult);
                }
            }
        }

    }

}
